package com.controller;

import com.entity.User;
import com.service.IUserService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sysUser")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {

        List<User> list = userService.selectUserList(user);
        if (list.size()>0){
            session.setAttribute("user",list.get(0));
            return  "/index/indexAdmin";
        }else {
            session.setAttribute("msg","密码或用户名错误");
            return  "/login/login";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
            session.invalidate();
            return  "/login/login";

    }

    @RequestMapping("/register")
    public String register(User user, HttpSession session) {

        int i = userService.insertUser(user);

        if (i>0){
            session.setAttribute("msg","注册成功，登陆吧");
            return  "/login/login";
        }else {
            session.setAttribute("msg","重新注册");
            return  "/login/register";
        }
    }
}
